<!--
 * Description: 报价gcm-commodity映射修改页面
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Dongxu.Ni
 * Date: 2020/04/15 01:30
-->

<template>
  <div>
    <!--数据表单-->
    <el-form label-width="100px" :model="formData" size="mini">
      <el-row>
        <!--采购分类-->
        <el-col :span="24">
          <el-form-item :label="$t('GcmCmtyDivide.commodity')">
            <el-input v-model="formData.commodity" clearable disabled />
          </el-form-item>
        </el-col>

        <!--GCM功能-->
        <el-col :span="24">
          <el-form-item :label="$t('GcmCmtyDivide.gcmFunction')">
            <el-input v-model="formData.gcmFunction" clearable disabled />
          </el-form-item>
        </el-col>

        <!--GCM Owner-->
        <el-col :span="24">
          <el-form-item :label="$t('GcmCmtyDivide.gcmOwner')">
            <el-select
              v-model="gcmOwnersList"
              clearable
              filterable
              multiple
              :placeholder="$t('select.placeholder')"
              style="width: 100%"
            >
              <el-option
                v-for="item in gcmOwerListByGcmFunction"
                :key="item.userName"
                :label="item.userName"
                :value="item.userId"
              >
                <span style="float: left;">{{ item.userName }}</span>
                <span style="float: right; color: #b4b4b4; font-size: 12px;margin-right: 25px">
                  {{ item.gcmFunction }}
                </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span style="display: block;text-align: center;margin-top: 5px">
      <!--取消按钮-->
      <el-button size="medium" @click="cancel">{{ $t('button.cancel') }}</el-button>
      <!--保存按钮-->
      <el-button :loading="isRequesting" size="medium" type="primary" @click="save">
        {{ $t('button.save') }}
      </el-button>
    </span>
  </div>
</template>

<script>
import { gcmOwnerCommodityMpEdit } from '@/api/quote/gcmOwner-cmty'

export default {
  name: 'GcmCmtyDivideEdit',
  props: {
    inputData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // 正在查询
      isRequesting: false,
      formData: {},
      gcmOwnersList: [],
      gcmOwerListByGcmFunction: []
    }
  },
  mounted() {
    this.pageDataInit()
  },
  methods: {
    pageDataInit() {
      this.formData = this.$deepCopy(this.inputData)
      this.gcmOwnersList = this.inputData.gcmOwners ? this.inputData.gcmOwners.split(',') : []
      this.gcmOwerListByGcmFunction = this.$store.getters[
        'user/getUserSimpleCollectsByGcmFunction'
      ](this.inputData.gcmFunction)
    },
    async save() {
      try {
        this.isRequesting = true
        const data = { commodity: this.inputData.commodity, gcmOwners: this.gcmOwnersList }
        await gcmOwnerCommodityMpEdit(data, { msgSuccess: true, msgError: true })
        this.$emit('save')
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>

<style scoped></style>
